﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Services;
using System.Xml;
using WebServiceKFC.Model;
using WebServiceKFC.MonAnF;
using WebServiceKFC.ThanhVienF;

namespace WebServiceKFC
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {
        DatabaseKFCEntities db = new DatabaseKFCEntities();
        
        List<MonAnDTO> list_ma = new List<MonAnDTO>();
      
        public Service1()
        {
            MonAnFactoryDTO factory = new MonAnFactoryDTO();
            list_ma = factory.LayDSMonAn();
        }

        [WebMethod]
        public SafeMonAnDTO[] GetDSMon()
        {
            SafeMonAnDTO[] kq;
            kq = new SafeMonAnDTO[list_ma.Count];
            for (int i = 0; i < kq.Length; i++)
            {
                kq[i] = new SafeMonAnDTO();
                kq[i].Attach(list_ma[i]);
            }
            return kq;
        }

        [WebMethod]
        public int TinhTienChoThanhVien(int theID, int tienHoaDonLucChuaGiamGia)
        {

            int tienHoaDonDaGiamGia = tienHoaDonLucChuaGiamGia;
            foreach (ThanhVien tv in db.ThanhViens)
            {
                
                if (theID == tv.theID)
                {
                    if (tv.diem >= 2000)
                    {
                        ThanhVienDTO thanhVien = new ThanhVienVIPDTO();
                        thanhVien.TheID = tv.theID;
                        thanhVien.Diem = tv.diem;
                        thanhVien.PhanTramGiamGia = tv.phanTramGiamGia;
                        tienHoaDonDaGiamGia = thanhVien.TinhTien(tienHoaDonLucChuaGiamGia);
                    }
                    else
                    {
                        ThanhVienDTO thanhVien = new ThanhVienThuongDTO();
                        thanhVien.TheID = tv.theID;
                        thanhVien.Diem = tv.diem;
                        thanhVien.PhanTramGiamGia = tv.phanTramGiamGia;
                        tienHoaDonDaGiamGia = thanhVien.TinhTien(tienHoaDonLucChuaGiamGia);
                    }
                }
                
            }
            return tienHoaDonDaGiamGia;
        }

        [WebMethod]
        public void ThemMonAn(SafeMonAnDTO safeMonAn)
        {
            MonAnDTO monAn = MonAnFactoryDTO.Produce(safeMonAn.LoaiDoiTuong);
            monAn.SetInforFrom(safeMonAn);
            MonAn ma = new MonAn();
            monAn.Save(db, ma);
            db.SaveChanges();
        }

        [WebMethod]
        public bool HuyMonAn(string maMonAn)
        {
            List<MonAn> list = (from ma in db.MonAns
                                where ma.MaMonAn == maMonAn
                                select ma).ToList();
            if (list.Count == 0)
                return false;


            // xoa mon an don neu la mon combo
            List<DSMonAnDon> dsMonAnDon = (from ma in db.DSMonAnDons
                                           where ma.MaMonAn == maMonAn || ma.MaMonAnDon == maMonAn
                                           select ma).ToList();

            if (dsMonAnDon.Count != 0)
            {
                foreach (var i in dsMonAnDon)
                {
                    db.DSMonAnDons.Context.DeleteObject(i);
                }
            }
          
            // xoa mon an chinh
            foreach (var i in list)
            {
                db.MonAns.Context.DeleteObject(i);
            }
            db.SaveChanges();
            return true;
        }

        [WebMethod]
        public int ThemThanhVien()
        {
            ThanhVien thanhVien = new ThanhVien();
            thanhVien.diem = 0;
            thanhVien.phanTramGiamGia = 0;
            db.AddToThanhViens(thanhVien);
            db.SaveChanges();
            return db.ThanhViens.Count();
        }
        
        [WebMethod]
        public List<SafeThanhVienDTO> LayDSThanhVien()
        {
            List<SafeThanhVienDTO> kq = new List<SafeThanhVienDTO>();
            List<ThanhVien> list = db.ThanhViens.ToList();
            foreach (var i in list)
            {
                SafeThanhVienDTO s = new SafeThanhVienDTO();
                s.Attach(i);
                kq.Add(s);
            }
            return kq;
        }

        [WebMethod]
        public MonAn CapNhatGia(string maMonAn, int gia)
        {
            MonAn m = db.MonAns.FirstOrDefault(aa => aa.MaMonAn == maMonAn);
            if (m == null)
                return null;
            m.DonGia = gia;
            db.SaveChanges();
            return db.MonAns.FirstOrDefault(aa => aa.MaMonAn == maMonAn);
        }
    }
}